package com.ylx.web.p;

import com.ylx.pojo.Order;
import com.ylx.pojo.User;
import com.ylx.service.OrderService;
import com.ylx.service.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/orderListServlet")
public class OrderListServlet extends HttpServlet {
    protected void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String s = request.getParameter("currentPage");
        int currentPage = Integer.parseInt(s);
        OrderService orderService = new OrderServiceImpl();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            response.sendRedirect("front/login.jsp");
        } else {
            List<Order> orderList = orderService.getOrderByUid(user.getUid(),null,null);

            List<Order> orderList1= new ArrayList<Order>();
            int n = 5;
            int totalPage = (orderList.size() - 1) / n + 1;
            for (int i = (currentPage - 1) * n; i < currentPage * n && i < orderList.size(); i++) {
                orderList1.add(orderList.get(i));
            }
            request.setAttribute("currentPage", currentPage);
            request.setAttribute("totalPage", totalPage);
            request.setAttribute("orderList", orderList1);
            request.getRequestDispatcher("front/order_list.jsp").forward(request, response);

        }


    }

    protected void doPost (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doGet(request, response);

    }
}
